package io.nutz.nutzsite.common.utils;

import java.util.TreeMap;

import org.nutz.http.Header;
import org.nutz.http.Http;
import org.nutz.http.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

/**
 * 获取地址类
 */
public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    public static final String IP_URL ="http://ip.tianqiapi.com/";// "http://ip.taobao.com/service/getIpInfo.php";

    public static String getRealAddressByIP(String ip) {
        String address = "XX XX";
        // 内网不查询
        if (IpUtils.internalIp(ip)) {
            return "内网IP";
        }
        try {
            
            Response rspStr = Http.get(IP_URL + "?ip=" + ip, 5 * 1000);
            if (!rspStr.isOK()) {
                log.error("获取地理位置异常 {}", ip);
                return address;
            }
            JSONObject obj = JSONObject.parseObject(rspStr.getContent());//
            JSONObject data = obj.getObject("data", JSONObject.class);
            String region = data.getString("region");
            String city = data.getString("city");
            address = region + " " + city;
        } catch (Exception e) {
            log.error("IP查询失败:" + ip + e.getMessage());
        }
        return address;
    }
    public static String getRealAddressByIP2(String ip) {
        String address = "XX XX";
        // 内网不查询
        if (IpUtils.internalIp(ip)) {
            return "内网IP";
        }
        try {
        	Header header = Header.create();
            //header.set("HTTP/1.1");
            header.set("Content-Type", "application/x-www-form-urlencoded");
            header.set("Accept", "*/*");
            header.set("User-Agent", "P5HPC");
            //header.set("Content-Type","text/html");
            header.set("Content-Length", "");
            header.set("Cache-Control", "no-cache");
            header.set("Connection", "close");
            header.set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            TreeMap map = new TreeMap();
            map.put("ip", ip);
            String resultJson = (Http.post3(IP_URL, map, header, 20 * 1000, 180 * 1000)).getContent();
            JSONObject data = JSONObject.parseObject(resultJson);//rspStr.getContent()

            /*Response rspStr = Http.get(IP_URL + "?ip=" + ip, 5 * 1000);
            if (!rspStr.isOK()) {
                log.error("获取地理位置异常 {}", ip);
                return address;
            }
            JSONObject obj = JSONObject.parseObject(rspStr.getContent());*/
            //JSONObject data = obj.getObject("data", JSONObject.class);
            String region = data.getString("province");
            String city = data.getString("city");
            address = region + " " + city;
        } catch (Exception e) {
            log.error("IP查询失败:" + ip + e.getMessage());
        }
        return address;
    }
  //Unicode转中文方法
    private static String unicodeToCn(String unicode) {
        /** 以 \ u 分割，因为java注释也能识别unicode，因此中间加了一个空格*/
        String[] strs = unicode.split("\\\\u");
        String returnStr = "";
        // 由于unicode字符串以 \ u 开头，因此分割出的第一个字符是""。
        for (int i = 1; i < strs.length; i++) {
            returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
        }
        return returnStr;
    }
}
